package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {



    @Autowired
    private ShoppingCartService shoppingCartService;


    @PostMapping("/add")
    public R addCart(@RequestBody ShoppingCart shoppingCart){
        return shoppingCartService.addCart(shoppingCart);
    }


    @PostMapping("/sub")
    public R subCart(@RequestBody ShoppingCart shoppingCart){

        return shoppingCartService.subCart(shoppingCart);
    }




    @GetMapping("/list")
    public R list(){
        //select * from xxx where user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw  = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.getUserId());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }


    @DeleteMapping("/clean")
    public R clearCart(){
        LambdaQueryWrapper<ShoppingCart> qw = new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId,UserHolder.getUserId());
        boolean remove = shoppingCartService.remove(qw);
        return remove?R.success("清空购物车成功"):R.error("清空购物车失败");
    }
}
